// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: hgpark

#include "coin2/strategy/rmm_strat/util/helper.h"

namespace coin2::strategy::rmm_strat {
void SetFeedTimeToLatency(
    const FeedUpdate& upd,
    coin::proto::LatencyProto::LatencyTag tag,
    const coin::proto::AccountRequestProto& acc_request,
    bool mark_end,
    exchange::base::strategy_util::LatencyRecorder* latency_recorder) {
  latency_recorder->Begin(tag);
  auto latency_context_id = latency_recorder->GetCurrentContextId();
  if (upd.ts().pre_read_available() > 0) {
    latency_recorder->SetProtoAndTime(
        latency_context_id,
        coin::proto::LifeOfSignal::RAW_FEED_READ_STARTED,
        upd.ts().pre_read_timestamp(),
        acc_request);
    latency_recorder->SetProtoAndTime(
        latency_context_id,
        coin::proto::LifeOfSignal::RAW_FEED_PRE_READ_AVAILABLE,
        upd.ts().pre_read_available(),
        acc_request);
  }
  latency_recorder->SetProtoAndTime(
      latency_context_id,
      coin::proto::LifeOfSignal::RAW_FEED_EXCHANGE_PUBLISHED,
      upd.ts().exchange_publish_timestamp(),
      acc_request);
  latency_recorder->SetProtoAndTime(
      latency_context_id,
      coin::proto::LifeOfSignal::RAW_FEED_RECEIVED,
      upd.ts().raw_rx_timestamp(),
      acc_request);
  latency_recorder->SetProtoAndTime(
      latency_context_id,
      coin::proto::LifeOfSignal::RAW_FEED_PARSE_STARTED,
      upd.ts().main_rx_timestamp(),
      acc_request);
  latency_recorder->SetProtoAndTime(
      latency_context_id,
      coin::proto::LifeOfSignal::ON_BOOK_FEED,
      upd.ts().main_tx_timestamp(),
      acc_request);
  if (mark_end) {
    latency_recorder->End(latency_context_id);
  }
}

}  // namespace coin2::strategy::rmm_strat
